namespace DotNetNuke.Modules.Admin.ResourceInstaller
{
    using ICSharpCode.SharpZipLib.Zip;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.IO;

    public class PaFile : ResourceInstallerBase
    {
        private byte[] _Buffer;
        private PaTextEncoding _Encoding;
        private string _Name;
        private string _Path;
        private PaFileType _Type;

        public PaFile(ZipInputStream unzip, ZipEntry entry)
        {
            string s = entry.Name;
            int i = s.LastIndexOf('/');
            if (i < 0)
            {
                this._Name = s.Substring(0, s.Length);
                this._Path = "";
            }
            else
            {
                this._Name = s.Substring(i + 1, s.Length - (i + 1));
                this._Path = s.Substring(0, i);
            }
            this._Buffer = new byte[(Convert.ToInt32(entry.Size) - 1) + 1];
            int size = 0;
            while (size < this._Buffer.Length)
            {
                size += unzip.Read(this._Buffer, size, this._Buffer.Length - size);
            }
            if (size != this._Buffer.Length)
            {
                throw new Exception(base.EXCEPTION_FileRead + Conversions.ToString(this._Buffer.Length) + "/" + Conversions.ToString(size));
            }
            string ext = this.Extension;
            this._Encoding = PaTextEncoding.Unknown;
            string refStringHelperL0 = ext.ToLower();
            if (refStringHelperL0 == "dnn")
            {
                this._Type = PaFileType.Dnn;
            }
            else if (refStringHelperL0 == "dll")
            {
                this._Type = PaFileType.Dll;
            }
            else if (refStringHelperL0 == "ascx")
            {
                this._Type = PaFileType.Ascx;
            }
            else if (refStringHelperL0 == "sql")
            {
                this._Type = PaFileType.Sql;
                this._Encoding = this.GetTextEncodingType(this._Buffer);
            }
            else if (ext.ToLower().EndsWith("dataprovider"))
            {
                this._Type = PaFileType.DataProvider;
                this._Encoding = this.GetTextEncodingType(this._Buffer);
            }
            else
            {
                this._Type = PaFileType.Other;
            }
        }

        private PaTextEncoding GetTextEncodingType(byte[] Buffer)
        {
            int endLoopIntHelperS0;
            if ((Buffer[0] == 0xff) & (Buffer[1] == 0xfe))
            {
                return PaTextEncoding.UTF16LittleEndian;
            }
            if ((Buffer[0] == 0xfe) & (Buffer[1] == 0xff))
            {
                return PaTextEncoding.UTF16BigEndian;
            }
            if (((Buffer[0] == 0xef) & (Buffer[1] == 0xbb)) & (Buffer[2] == 0xbf))
            {
                return PaTextEncoding.UTF8;
            }
            int i = 0;
            do
            {
                if (Buffer[i] > 0x7f)
                {
                    return PaTextEncoding.Unknown;
                }
                i++;
                endLoopIntHelperS0 = 100;
            }
            while (i <= endLoopIntHelperS0);
            return PaTextEncoding.UTF7;
        }

        public byte[] Buffer
        {
            get
            {
                return this._Buffer;
            }
        }

        public PaTextEncoding Encoding
        {
            get
            {
                return this._Encoding;
            }
        }

        public string Extension
        {
            get
            {
                string ext = System.IO.Path.GetExtension(this._Name);
                if ((ext == null) | (ext.Length == 0))
                {
                    return "";
                }
                return ext.Substring(1);
            }
        }

        public string FullName
        {
            get
            {
                return System.IO.Path.Combine(this._Path, this._Name);
            }
        }

        public string Name
        {
            get
            {
                return this._Name;
            }
        }

        public string Path
        {
            get
            {
                return this._Path;
            }
            set
            {
                this._Path = value.Trim(new char[] { '/', '\\' });
            }
        }

        public PaFileType Type
        {
            get
            {
                return this._Type;
            }
        }
    }
}

